
# ON THE SERVER

import sqlite3
print sqlite3.version
conn = sqlite3.connect('C:\\Users\\akiphtj\\AppData\\Local\\DexenOrg\\Dexen\\server.db')
c = conn.cursor()

"""
c.execute("SELECT * FROM sqlite_master WHERE type='table'")
rows = c.fetchall()
for row in rows:
	print row
"""

sql_query = "SELECT * FROM ARCHIVE WHERE NAME='phenotype.zip' AND JOBID=75 AND INDID=100"
print sql_query
c.execute(sql_query)
archive = c.fetchone()
ind_id = archive[0]
archive_content = archive[2]


# IN THE HOUDINI OTL

import zlib
import zipfile
import os
import StringIO

folder_path = "d:\\temp\\temp23\\dexen_data"

zip_file_content = zlib.decompress(archive_content)
in_mem_zip_file = StringIO.StringIO(zip_file_content)
zf = zipfile.ZipFile(in_mem_zip_file, "r")
for zf_info in zf.filelist:
	print zf_info
	zip_path = os.path.join(folder_path, str(ind_id) + "_" + zf_info.filename)
	with open(zip_path, "wb") as f:
		f.write(zf.read(zf_info))
zf.close()


conn.close()
print "done"